package com.lyl.jingdongsearch.controller;

import com.lyl.jingdongsearch.domain.Content;
import com.lyl.jingdongsearch.service.ContentServiceImpl;
import org.elasticsearch.action.search.SearchRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author lylbell
 * @date 2021-09-26 16:06
 */
@RestController
public class ContentController {

    @Autowired
    ContentServiceImpl contentService;


    /**
     * 解析content，添加到es中
     * @param keyword
     * @return
     * @throws IOException
     */
    @GetMapping("/parse/{keyword}")
    public Boolean parseContent(@PathVariable("keyword") String keyword) throws IOException {
        return contentService.parseContent(keyword);
    }

    /**
     * 搜索es
     * @param keyword
     * @param page
     * @param size
     * @return
     * @throws IOException
     */
    @GetMapping("/search/{keyword}/{page}/{size}")
    public List<Map<String,Object>> searchContent(@PathVariable("keyword") String keyword,
                                                  @PathVariable("page") int page,
                                                  @PathVariable("size") int size) throws IOException {
        return contentService.searchContent(keyword,page,size);
    }


    /**
     * 高亮搜索es
     * @param keyword
     * @param page
     * @param size
     * @return
     * @throws IOException
     */
    @GetMapping("/search/highlight/{keyword}/{page}/{size}")
    public List<Map<String,Object>> searchHigLightContent(@PathVariable("keyword") String keyword,
                                                  @PathVariable("page") int page,
                                                  @PathVariable("size") int size) throws IOException {
        return contentService.searchHigLightContent(keyword,page,size);
    }
}
